import { createRouter, createWebHashHistory } from 'vue-router'
import * as hooks from './hooks'

const routers = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: '大气层',
      component: () => import('../views/container_layout.vue'),
      redirect: 'index',
      children: [
        {
          path: 'index',
          name: 'index',
          component: () => import('../views/index/index.vue'),
          meta: {
            title: '首页',
            islogin: true
          }
        },
        {
          path: 'room_management',
          name: 'room_management',
          meta: {
            title: '房型管理'
          },
          component: () => import('../views/room_management/room_management_layout.vue'),
          children: [
            {
              path: 'house_shaped',
              name: 'house_shaped',
              component: () => import('../views/room_management/house_shaped.vue'),
              meta: {
                title: '房型管理',
                islogin: true
              }
            },
            {
              path: 'room_number',
              name: 'room_number',
              component: () => import('../views/room_management/room_number.vue'),
              meta: {
                title: '房号管理',
                islogin: true
              }
            },
            {
              path: 'room_state',
              name: 'room_state',
              component: () => import('../views/room_management/room_state.vue'),
              meta: {
                title: '房态管理',
                islogin: true
              }
            }
          ]
        },
        {
          path: 'vip_management',
          name: 'vip_management',
          meta: {
            title: '会员管理',
            islogin: true
          },
          component: () => import('../views/vip_management/vip_management_layout.vue'),
          children: [
            {
              path: 'vip_list',
              name: 'vip_list',
              component: () => import('../views/vip_management/vip_list.vue'),
              meta: {
                title: '会员列表',
                islogin: true
              }
            },
            {
              path: 'integral_setting',
              name: 'integral_setting',
              component: () => import('../views/vip_management/integral_setting.vue'),
              meta: {
                title: '积分设置',
                islogin: true
              }
            },
            {
              path: 'consumption_recordd',
              name: 'consumption_recordd',
              component: () => import('../views/vip_management/consumption_recordd.vue'),
              meta: {
                title: '消费记录',
                islogin: true
              }
            },
            {
              path: 'recharge_recordd',
              name: 'recharge_recordd',
              component: () => import('../views/vip_management/recharge_recordd.vue'),
              meta: {
                title: '充值记录',
                islogin: true
              }
            }
          ]
        },
        {
          path: 'card',
          name: 'card',
          meta: {
            title: '优惠券',
            islogin: true
          },
          component: () => import('../views/card/card_layout.vue'),
          children: [
            {
              path: 'card_list',
              name: 'card_list',
              component: () => import('../views/card/card_list.vue'),
              meta: {
                title: '卡券列表',
                islogin: true
              }
            },
            {
              path: 'add_card',
              name: 'add_card',
              component: () => import('../views/card/add_card.vue'),
              meta: {
                title: '增加卡券',
                islogin: true
              }
            },
            {
              path: 'claim_card',
              name: 'claim_card',
              component: () => import('../views/card/claim_card.vue'),
              meta: {
                title: '卡券领取列表',
                islogin: true
              }
            }
          ]
        },
        {
          path: 'settings',
          name: 'settings',
          meta: {
            title: '设置',
            islogin: true
          },
          component: () => import('../views/settings/setting_layout.vue'),
          children: [
            {
              path: 'hotel_setting',
              name: 'hotel_setting',
              component: () => import('../views/settings/hotel_setting.vue'),
              meta: {
                title: '酒店设置',
                islogin: true
              }
            }
          ]
        },
        {
          path: 'user_management',
          name: 'user_management',
          component: () => import('../views/user_management/user_management_layout.vue'),
          meta: {
            title: '用户管理',
            islogin: true
          },
          children: [
            {
              path: 'administrator_list',
              name: 'administrator_list',
              component: () => import('../views/user_management/administrator_list.vue'),
              meta: {
                title: '管理员列表',
                islogin: true
              }
            },
            {
              path: 'role_listd',
              name: 'role_listd',
              component: () => import('../views/user_management/role_listd.vue'),
              meta: {
                title: '角色列表',
                islogin: true
              }
            }
          ]
        }
      ]
    },
    // 登陆页面是全屏显示,所以不在大壳中
     {
      path: '/login',
      name: '登陆页',
      hidden: true,
      component: () => import('../views/login/login.vue')
    },
    /*{
      path: '/regist',
      name: '注册页',
      hidden: true,
      component: () => import('../views/login_regist/login-page.vue')
    },
    // 写在最后，上面的路由都没有匹配上，去首页
    {
      path: '/:aaa',
      redirect: (to) => {
        return {
          path: '/index'
        }
      }
    }, */

  ]
})

// 路由守卫，beforeEach是前置守卫，每次切换路由之前会执行
// 必须next()放行才能真正实现页面跳转
/* router.beforeEach((to, from, next) => {
  // 登录页肯定能看。to表示要去的页面
  if (to.name == '登陆页' || to.name == '注册页') {
    // next()没有参数表示放行到用户要去的页面
    next()
  } else {
    // 读取本地存储中的token，如果没有token就跳转到login
    // 不验证token合法
    let token = localStorage.getItem('token')
    // 如果本地存储器中token是空，那么就强制到登录页
    if (token == null) {
      // 强制跳转到登录页
      next({ name: '登陆页' })
    } else {
      // 有token就放行，不检查合法性。粗略检查。
      next()
    }
  }
}) */
Object.keys(hooks).forEach(key => {
  routers.beforeEach(hooks[key]())
})
export default routers
